package com.guan.eduservice.controller;


import com.guan.commonutils.R;
import com.guan.eduservice.entity.EduChapter;
import com.guan.eduservice.entity.chapter.ChapterVo;
import com.guan.eduservice.service.EduChapterService;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程章节表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-01-01
 */
@RestController
@RequestMapping("/eduservice/chapter/*")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;


    /**
     * 根据课程id查询课程大纲列表
     * @return
     */
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId){
        List<ChapterVo> list = chapterService.getChapterVideoBycourseId(courseId);

        return R.ok().data("allChapterVideo",list);
    }

    /**
     * 添加章节
     * @param eduChapter
     * @return
     */
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter){
        chapterService.save(eduChapter);
        return R.ok();
    }

    /**
     * 根据章节id查询信息
     * @param chapterId
     * @return
     */
    @GetMapping("getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId){
        EduChapter eduChapter = chapterService.getById(chapterId);
        return R.ok().data("chapter",eduChapter);
    }

    /**
     * 修改章节
     * @param eduChapter
     * @return
     */
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        chapterService.updateById(eduChapter);
        return R.ok();
    }

    /**
     * 删除章节 第3方案
     * 1. 如果章节中没有小节，可以直接删除
     * 2. 如果存在小节，删章节的同时把小节也删除，
     * 3. 不让删除章节，只有删除小节后才可以删除章节
     * @param chapterId
     * @return
     */
    @DeleteMapping("{chapterId}")
    public R deleteChapetr(@PathVariable String chapterId){
        boolean flag = chapterService.deleteChapter(chapterId);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

}

